*** Generating figure 3: Baseline results for the full sample ***

** Preamble **

clear all
set more off


local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/figures"

* Note: local directory is the output folder 
cd "$path_output"

				** Loading the input file **

use `path_input'/dataset_final_may20, clear

estimates clear

xtset codigo_ibge turno

local baseline_controls "pop1991 years_sch1991 renda1991 sh_tv1991 sh_agua1991 sh_elect1991 sh_rural"
local CGB_FEs			"pct_sh_tv1991 pct_sh_rural pct_renda1991"

/*generating percentiles*/
foreach var in `baseline_controls' {
xtile pct_`var'=  `var', n(10)
replace pct_`var'=0 if turno==2
}

/*generating pscores*/
encode uf, gen(UF)
psmatch2 Globo T1* if turno==1,  noreplace neighbor(1)
egen ppp=mean(_pscore), by(codigo)
gen pscore=1
replace pscore=ppp/(1-ppp) if Globo==0

label var treatment  "Globo"
label var treatment1 "Globo and Others"
label var treatment2 "Globo Only"

									 *** Graph A: votes for left-right ***

												** I **	

** spec 1: No controls **
							
eststo: xi: reghdfe sh_Tesquerda treatment turno            , vce(cluster codigo_ibge) a(codigo_ibge)

** spec 2: Group FE **
					
eststo: xi: reghdfe sh_Tesquerda treatment turno            , vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')

** spec 3: Reweighting **
					
eststo: xi: reghdfe sh_Tesquerda treatment turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)


												** II **	

** spec 1: No controls **
							
eststo: xi: reghdfe sh_Tdireita treatment turno            , vce(cluster codigo_ibge) a(codigo_ibge)

** spec 2: Group FE **
					
eststo: xi: reghdfe sh_Tdireita treatment turno            , vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')

** spec 3: Reweighting **
					
eststo: xi: reghdfe sh_Tdireita treatment turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)


								*** Graph B: votes for Lula-Collor ***


											** I **
						
** spec 1: No controls **
							
eststo: xi: reghdfe sh_Tpt_ treatment sh_Tpsp_-sh_Tpsdb_ turno            ,	vce(cluster codigo_ibge) a(codigo_ibge)
					
** spec 2: Group FE **
					
eststo: xi: reghdfe sh_Tpt_ treatment sh_Tpsp_-sh_Tpsdb_ turno            , vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')

** spec 3: Reweighting **
					
eststo: xi: reghdfe sh_Tpt_ treatment sh_Tpsp_-sh_Tpsdb_ turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)


											** II **
						
** spec 1: No controls **
							
eststo: xi: reghdfe sh_Tprn_ treatment sh_Tpsp_-sh_Tpsdb_ turno            , vce(cluster codigo_ibge) a(codigo_ibge)
					
** spec 2: Group FE **
					
eststo: xi: reghdfe sh_Tprn_ treatment sh_Tpsp_-sh_Tpsdb_ turno            , vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')

** spec 3: Reweighting **
					
eststo: xi: reghdfe sh_Tprn_ treatment sh_Tpsp_-sh_Tpsdb_ turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)
				
				
********************************************************************************

						*** Graph: Main results, Left vs Right

													** Graph A **

coefplot (est1, msymbol(D) color(black) offset(0.00) ciopts(recast(rcap) lcol(black)) text( .006  1.23 "Votes for left-wing candidates",  size(small))  )  ///                   
		 (est2, msymbol(S) color( navy) offset(0.25) ciopts(recast(rcap) lcol( navy)) 				  						                            )  ///
		 (est3, msymbol(C) color(  red) offset(0.50) ciopts(recast(rcap) lcol(  red)) 											     	                )  ///
		 (est4, msymbol(D) color(black) offset(1.50) ciopts(recast(rcap) lcol(black)) text( -.02  2.67 "Votes for right-wing candidates", size(small))  )  ///                   
		 (est5, msymbol(S) color( navy) offset(1.75) ciopts(recast(rcap) lcol( navy))											                        )  ///     
		 (est6, msymbol(C) color(  red) offset(2.00) ciopts(recast(rcap) lcol(  red)) 				 								                    ), ///
		 ///
		 vertical  						 					          			        ///
		 keep(treatment)                 					  		  			        /// 
		 yline(0, lpattern(dash) lcolor(grey))  	        		  			        ///
		 ylabel(      -0.05(0.02)0.05) 											   		///
		 yscale(range(-0.05(0.02)0.05))											   		///
		 graphregion(color(white))           	                      			        ///
		 xlab("")                 		                       		  			        ///
		 plotlabels("Unconditional" "Group FEs" "Reweighted" " " " " " " " " " " " ")   ///
		 legend(rows(1))																///
		 saving("Figure_baseline_Vote_Shares_Full_Sample_Graph_A.png", replace) 
		 
		 graph export Figure_baseline_Vote_Shares_Full_Sample_Graph_A.png, replace

		 
	 
	 
													** Graph B **

coefplot (est7, msymbol(D) color(black) offset(0.00) ciopts(recast(rcap) lcol(black))  text( .003  1.23 "Lula's vote share",  size(small))   			 )  ///                   
		 (est8, msymbol(S) color( navy) offset(0.25) ciopts(recast(rcap) lcol( navy)) 				  						                             )  ///
		 (est9, msymbol(C) color(  red) offset(0.50) ciopts(recast(rcap) lcol(  red)) 											     	                 )  ///
		 (est10, msymbol(D) color(black) offset(1.50) ciopts(recast(rcap) lcol(black)) text(-.006  2.77 "Collor's vote share", size(small))  			 )  ///                   
		 (est11, msymbol(S) color( navy) offset(1.75) ciopts(recast(rcap) lcol( navy))											                         )  ///     
		 (est12, msymbol(C) color(  red) offset(2.00) ciopts(recast(rcap) lcol(  red)) 				 								                     ), ///
		 ///
		 vertical  						 					          			        ///
		 keep(treatment)                 					  		  			        /// 
		 yline(0, lpattern(dash) lcolor(grey))  	        		  			        ///								 
		 ylabel(      -0.05(0.02)0.05) 											   		///
		 yscale(range(-0.05(0.02)0.05))											   		///
		 graphregion(color(white))           	                      			        ///
		 xlab("")                 		                       		  			        ///
		 plotlabels("Controls" "Group FEs" "Reweighted" " " " " " " " " " " " ")   		///
		 saving("Figure_baseline_Vote_Shares_Full_Sample_Graph_B.png", replace) 		///
		 legend(rows(1))
		 
		 graph export Figure_baseline_Vote_Shares_Full_Sample_Graph_B.png, replace
		 
 
